//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package ciicos.alarm.controller;

import ciicos.alarm.entity.AlarmDto;
import ciicos.alarm.entity.AlarmHisNDataEntity;
import ciicos.alarm.model.alarmhisndata.AlarmHisNDataForm;
import ciicos.alarm.model.alarmhisndata.AlarmHisNDataInfoVO;
import ciicos.alarm.model.alarmhisndata.AlarmHisNDataListVO;
import ciicos.alarm.model.alarmhisndata.AlarmHisNDataPagination;
import ciicos.alarm.service.AlarmHisNDataService;
import ciicos.base.ActionResult;
import ciicos.base.UserInfo;
import ciicos.base.vo.PageListVO;
import ciicos.base.vo.PaginationVO;
import ciicos.exception.DataException;
import ciicos.permission.entity.UserEntity;
import ciicos.util.DateUtil;
import ciicos.util.GeneraterSwapUtil;
import ciicos.util.JsonUtil;
import ciicos.util.RandomUtil;
import ciicos.util.StringUtil;
import ciicos.util.UserProvider;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Executor;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(
    tags = {"新历史报警"},
    value = "alarmManage"
)
@RequestMapping({"/api/alarmManage/AlarmHisNData"})
public class AlarmHisNDataController {
    private static final Logger log = LoggerFactory.getLogger(AlarmHisNDataController.class);
    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;
    @Autowired
    private UserProvider userProvider;
    @Autowired
    private AlarmHisNDataService alarmHisNDataService;
    @Autowired
    private Executor threadPoolExecutor;

    @ApiOperation("获取列表")
    @PostMapping({"/getList"})
    public ActionResult list(@RequestBody AlarmHisNDataPagination alarmHisNDataPagination) throws IOException {
        List<AlarmHisNDataEntity> list = this.alarmHisNDataService.getList(alarmHisNDataPagination);
        List<AlarmHisNDataListVO> listVO = JsonUtil.getJsonToList(list, AlarmHisNDataListVO.class);

        for(AlarmHisNDataListVO alarmHisNDataVO : listVO) {
            alarmHisNDataVO.setTagname_name(alarmHisNDataVO.getTagname());
            alarmHisNDataVO.setTagdesc_name(alarmHisNDataVO.getTagdesc());
            alarmHisNDataVO.setTageu_name(alarmHisNDataVO.getTageu());
            alarmHisNDataVO.setTagarea_name(alarmHisNDataVO.getTagarea());
            alarmHisNDataVO.setTagunit_name(alarmHisNDataVO.getTagunit());
            alarmHisNDataVO.setTagtype_name(alarmHisNDataVO.getTagtype());
            alarmHisNDataVO.setAlarmtype_name(this.generaterSwapUtil.getDicName(alarmHisNDataVO.getAlarmtype(), "451412507504957189"));
            alarmHisNDataVO.setAlarmtime_name(alarmHisNDataVO.getAlarmtime() != null ? new Date(alarmHisNDataVO.getAlarmtime()) : null);
            alarmHisNDataVO.setAlarmv_name(alarmHisNDataVO.getAlarmv());
            alarmHisNDataVO.setAlarmlimitv_name(alarmHisNDataVO.getAlarmlimitv());
            alarmHisNDataVO.setAlarmsp_name(alarmHisNDataVO.getAlarmsp());
            alarmHisNDataVO.setAlarmlevel_name(this.generaterSwapUtil.getDicName(alarmHisNDataVO.getAlarmlevel(), "451405363372119813"));
            alarmHisNDataVO.setCreatoruserid_name(alarmHisNDataVO.getCreatoruserid());
            alarmHisNDataVO.setCreatortime_name(alarmHisNDataVO.getCreatortime() != null ? new Date(alarmHisNDataVO.getCreatortime()) : null);
            alarmHisNDataVO.setReturnv_name(alarmHisNDataVO.getReturnv());
            alarmHisNDataVO.setReturntime_name(alarmHisNDataVO.getReturntime() != null ? new Date(alarmHisNDataVO.getReturntime()) : null);
            alarmHisNDataVO.setLastmodifyuserid_name(this.generaterSwapUtil.userSelectValue(alarmHisNDataVO.getLastmodifyuserid()));
            alarmHisNDataVO.setLastmodifytime_name(alarmHisNDataVO.getLastmodifytime());
            alarmHisNDataVO.setConfirm_name(this.generaterSwapUtil.getDicName(alarmHisNDataVO.getConfirm(), "413671997705289733"));
            alarmHisNDataVO.setSendmark_name(this.generaterSwapUtil.getDicName(alarmHisNDataVO.getSendmark(), "415081677472137605"));
            alarmHisNDataVO.setEnabledmark_name(alarmHisNDataVO.getEnabledmark());
            alarmHisNDataVO.setAlarmstatus_name(this.generaterSwapUtil.getDicName(alarmHisNDataVO.getAlarmstatus(), "459255239141230853"));
            alarmHisNDataVO.setConfirmdesc_name(alarmHisNDataVO.getConfirmdesc());
        }

        PageListVO vo = new PageListVO();
        vo.setList(listVO);
        PaginationVO page = (PaginationVO)JsonUtil.getJsonToBean(alarmHisNDataPagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);
    }

    @PostMapping
    @Transactional
    @ApiOperation("创建")
    public ActionResult create(@RequestBody @Valid AlarmHisNDataForm alarmHisNDataForm) throws DataException {
        String b = this.alarmHisNDataService.checkForm(alarmHisNDataForm, 0);
        if (StringUtil.isNotEmpty(b)) {
            return ActionResult.fail(b + "不能重复");
        } else {
            String mainId = RandomUtil.uuId();
            UserInfo userInfo = this.userProvider.get();
            UserEntity userEntity = this.generaterSwapUtil.getUser(userInfo.getUserId());
            AlarmHisNDataEntity entity = (AlarmHisNDataEntity)JsonUtil.getJsonToBean(alarmHisNDataForm, AlarmHisNDataEntity.class);
            entity.setId(mainId);
            this.alarmHisNDataService.save(entity);
            return ActionResult.success("创建成功");
        }
    }

    @ApiOperation("信息")
    @GetMapping({"/{id}"})
    public ActionResult<AlarmHisNDataInfoVO> info(@PathVariable("id") String id) {
        AlarmHisNDataEntity entity = this.alarmHisNDataService.getInfo(id);
        AlarmHisNDataInfoVO vo = (AlarmHisNDataInfoVO)JsonUtil.getJsonToBean(entity, AlarmHisNDataInfoVO.class);
        vo.setLastmodifyuserid(this.generaterSwapUtil.userSelectValue(vo.getLastmodifyuserid()));
        if (vo.getLastmodifytime() != null) {
            vo.setLastmodifytime(vo.getLastmodifytime());
        }

        return ActionResult.success(vo);
    }

    @ApiOperation("表单信息(详情页)")
    @GetMapping({"/detail/{id}"})
    public ActionResult<AlarmHisNDataInfoVO> detailInfo(@PathVariable("id") String id) {
        AlarmHisNDataEntity entity = this.alarmHisNDataService.getInfo(id);
        AlarmHisNDataInfoVO vo = (AlarmHisNDataInfoVO)JsonUtil.getJsonToBean(entity, AlarmHisNDataInfoVO.class);
        vo.setTagname(vo.getTagname());
        vo.setTagdesc(vo.getTagdesc());
        vo.setTageu(vo.getTageu());
        vo.setTagarea(vo.getTagarea());
        vo.setTagunit(vo.getTagunit());
        vo.setTagtype(vo.getTagtype());
        vo.setAlarmtype(this.generaterSwapUtil.getDicName(vo.getAlarmtype(), "451412507504957189"));
        vo.setAlarmtime(vo.getAlarmtime());
        vo.setAlarmv(vo.getAlarmv());
        vo.setAlarmlimitv(vo.getAlarmlimitv());
        vo.setAlarmsp(vo.getAlarmsp());
        vo.setAlarmlevel(this.generaterSwapUtil.getDicName(vo.getAlarmlevel(), "451405363372119813"));
        vo.setCreatoruserid(vo.getCreatoruserid());
        vo.setCreatortime(vo.getCreatortime());
        vo.setReturnv(vo.getReturnv());
        vo.setReturntime(vo.getReturntime());
        vo.setLastmodifyuserid(this.generaterSwapUtil.userSelectValue(vo.getLastmodifyuserid()));
        vo.setLastmodifytime(vo.getLastmodifytime());
        vo.setConfirm(this.generaterSwapUtil.getDicName(vo.getConfirm(), "413671997705289733"));
        vo.setSendmark(this.generaterSwapUtil.getDicName(vo.getSendmark(), "415081677472137605"));
        vo.setEnabledmark(vo.getEnabledmark());
        vo.setAlarmstatus(this.generaterSwapUtil.getDicName(vo.getAlarmstatus(), "456723697751820677"));
        return ActionResult.success(vo);
    }

    @PutMapping({"/{id}"})
    @Transactional
    @ApiOperation("更新")
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid AlarmHisNDataForm alarmHisNDataForm) throws DataException {
        String b = this.alarmHisNDataService.checkForm(alarmHisNDataForm, 1);
        if (StringUtil.isNotEmpty(b)) {
            return ActionResult.fail(b + "不能重复");
        } else {
            UserInfo userInfo = this.userProvider.get();
            AlarmHisNDataEntity entity = this.alarmHisNDataService.getInfo(id);
            if (entity != null) {
                alarmHisNDataForm.setLastmodifyuserid(userInfo.getUserId());
                alarmHisNDataForm.setLastmodifytime(DateUtil.getNow());
                AlarmHisNDataEntity subentity = (AlarmHisNDataEntity)JsonUtil.getJsonToBean(alarmHisNDataForm, AlarmHisNDataEntity.class);
                boolean b1 = this.alarmHisNDataService.updateById(subentity);
                return !b1 ? ActionResult.fail("当前表单原数据已被调整，请重新进入该页面编辑并提交数据") : ActionResult.success("更新成功");
            } else {
                return ActionResult.fail("更新失败，数据不存在");
            }
        }
    }

    @ApiOperation("删除")
    @DeleteMapping({"/{id}"})
    @Transactional
    public ActionResult delete(@PathVariable("id") String id) {
        AlarmHisNDataEntity entity = this.alarmHisNDataService.getInfo(id);
        if (entity != null) {
            this.alarmHisNDataService.delete(entity);
        }

        return ActionResult.success("删除成功");
    }

    @PostMapping({"/updateAlarm"})
    @Transactional
    public ActionResult updateAlarm(@RequestBody AlarmDto param) {
        String id = param.getId();
        String confirmDesc = param.getConfirmDesc();
        Integer confirm = param.getConfirm();
        AlarmHisNDataEntity entity = this.alarmHisNDataService.getInfo(id);
        if (entity != null && StringUtil.isNotEmpty(confirmDesc)) {
            entity.setConfirmdesc(confirmDesc);
            entity.setConfirm(confirm);
            this.alarmHisNDataService.confirm(entity);
            return ActionResult.success("确认成功");
        } else if (param.getConfirm() == 4) {
            entity.setConfirmdesc(confirmDesc);
            entity.setConfirm(confirm);
            this.alarmHisNDataService.confirm(entity);
            return ActionResult.success("确认成功");
        } else {
            return ActionResult.fail("参数不全或者修改失败，请检查");
        }
    }

    @PostMapping({"/updateAlarmAll"})
    @Transactional
    public ActionResult updateAlarmAll(@RequestBody AlarmDto param) {
        List<String> ids = param.getIds();
        if (CollectionUtils.isEmpty(ids)) {
            return ActionResult.fail("数据不能为空");
        } else {
            for(String id : ids) {
                String confirmDesc = param.getConfirmDesc();
                Integer confirm = param.getConfirm();
                AlarmHisNDataEntity entity = this.alarmHisNDataService.getInfo(id);
                if (entity != null && StringUtil.isNotEmpty(confirmDesc)) {
                    entity.setConfirmdesc(confirmDesc);
                    entity.setConfirm(confirm);
                    this.alarmHisNDataService.confirm(entity);
                }
            }

            return ActionResult.success("更新成功");
        }
    }

    @PostMapping({"/one/click/process"})
    public ActionResult oneClickProcess(@RequestBody AlarmDto param) {
        return this.alarmHisNDataService.updateClikcProcess(param);
    }
}
